import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import vuexPersist from "vuex-persist"

export default new Vuex.Store({
  state: {
    username:"",
    shopcar:[]
  },
  mutations: {
    setuser(state,val){
      state.username=val
    },
    exit(state){
      state.username=""
    },
    add(state,item){
      var flag=false
      state.shopcar.forEach((i)=>{
        if(i.title==item.title){
          i.num++
          flag=true
        }
      })
      if(flag==false){
        Vue.set(item,"num",1)
        state.shopcar.push(item)
      }
    },
    changenum(){},
    remove(state,id){
      state.shopcar.forEach((i,index)=>{
        if(i.iid==id){
          state.shopcar.splice(index,1)
        }
      })
    }
  },
  actions: {
  },
  modules: {
  },plugins:[new vuexPersist({
    storage:window.localStorage,
  }).plugin,]
})
